package io.github.talelin.latticy.common;

import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.autoconfigure.exception.ParameterException;
import io.github.talelin.latticy.bo.PageCounterBO;
import io.github.talelin.latticy.model.GroupDO;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CommonUtil {


    /**
     * start转换page
     */
    public static PageCounterBO converToPageParamter(int start, int count) {
        int pageNum = start / count;
        return PageCounterBO.builder()
                .page(pageNum)
                .size(count)
                .build();
    }

    /**
     * 时间字符串转日期
     * @param date
     * @return
     */
    public static Date stringToDate1(String date) {
        Date now = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        try {
            now = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            throw new ParameterException(20000);
        }
        return now;
    }

    /**
     * 字符串转日期
     * @param date
     * @return
     */
    public static Date stringToDate2(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try {
            return sdf.parse(date);
        } catch (Exception e) {
            throw new ParameterException(20002);
        }
    }

    /**
     * 字符串转日期
     * @param date
     * @return
     */
    public static Date stringToDate3(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return sdf.parse(date);
        } catch (Exception e) {
            throw new ParameterException(20002);
        }
    }

    /**
     * 日期转字符串
     * @param date
     * @return
     */
    public static String dateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        try {
            return sdf.format(date);
        } catch (Exception e) {
            throw new ParameterException();
        }
    }

    /**
     * 比较时间
     * @param date1
     * @param date2
     * @return
     */
    public static Boolean compareTime(Date date1, Date date2) {
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        if (time2 > time1) {
            throw new ParameterException(20001);
        }
        return true;
    }


}
